package com.tuton.javanet.chapter03;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private int port = 8000;
	private ServerSocket serverSocket;
	
	public Server() throws IOException {
		//limited the number of the sockets from client
		serverSocket = new ServerSocket(port,3);
		System.out.println("The server was started....");
	}
	
	public static void main(String[] args) throws IOException, InterruptedException {
		new Server().doServerJob();
		Thread.sleep(60000*10);

	}

	private void doServerJob() {
		Socket socket = null;
		
		while(true) {
			try {
				socket = serverSocket.accept();
				System.out.println("Accept a socket from : " +
						socket.getInetAddress() + ":" +
						socket.getPort());
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if(socket != null) {
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
	}
}
